package com.hn658.base.message.frontend.action.query.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.base.message.frontend.action.query.IMessageQueryAction;
import com.hn658.base.message.frontend.action.query.vo.MessageQueryRequest;
import com.hn658.base.message.frontend.action.query.vo.MessageQueryResponse;
import com.hn658.base.message.notification.msg.business.IMessageManager;
import com.hn658.base.message.notification.msg.dto.MsgQueryDTO;
import com.hn658.base.message.notification.msg.entity.MessageEO;
import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.framework.shared.utils.BeanMapper;


/**
 * 消息查询服务
 *** 此查询包括单聊与群聊
 * @author lsj
 *
 */
@Controller("messageQueryAction")
@Path("query")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class MessageQueryAction extends AbstractBaseService implements IMessageQueryAction{
	@Autowired
	private IMessageManager messageManager;
	
	/**
	 * 查询群聊消息记录
	 * @param request
	 * @return
	 */
	@Path("queryGroupMessage")
	@POST
	public IServiceResponse queryGroupMessage(MessageQueryRequest request){
		MessageQueryResponse response=new MessageQueryResponse();
		
		MsgQueryDTO queryParam=new MsgQueryDTO();
		try{
			queryParam.setAppId(request.getAppId());
			queryParam.setFromUid(request.getFromUid());
			queryParam.setToGroupId(request.getToGroupId());
			if(request.getStart()!=null){
				queryParam.setStart(request.getStart());
			}
			GenericPage<MessageEO> queryPage = messageManager.queryGroupPage(queryParam);
			List<MessageEO> msgList = null;
			if(queryPage.getData()!=null){
				msgList = BeanMapper.mapList(queryPage.getData(), MessageEO.class);
			}
			response.setMsgList(msgList);
			response.setTotalCount(queryPage.getTotalCount());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
